﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;
using TSB.SharePointUrlShortener.DAL;

namespace TSB.SharePointUrlShortener.Layouts.TSB.SharePointUrlShortener
{
    public partial class Url_Shortener_Settings : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(QS_Url))
            {
                CheckUrlExists();
            }

            if (!IsPostBack)
            {
                GenerateDefaultShortcode();
            }
            ltSiteUrl.Text = string.Format("{0}/url/", SPContext.Current.Site.RootWeb.Url);
            ltUrl.Text = QS_Url;
            ltType.Text = QS_Type;
            btnCreate.Click += btnCreate_Click;
            btnDelete.Click += btnDelete_Click;
        }

        void btnDelete_Click(object sender, EventArgs e)
        {
            var urlShortenerRepository = new UrlShortenerRepository();
            var shortcode = string.Empty;

            if (!urlShortenerRepository.CheckUrlExists(QS_Url, out shortcode))
            {
                return;
            }

            if (urlShortenerRepository.DeleteByUrl(QS_Url))
            {
                SetupSuccessPanel("The short code has been successfully deleted");
            }
            else
            {
                ltErrors.Text = "Problem deleting Short Url";
            }
        }

        void CheckUrlExists()
        {
            var urlShortenerRepository = new UrlShortenerRepository();
            var shortcode = string.Empty;

            if (urlShortenerRepository.CheckUrlExists(QS_Url, out shortcode))
            {
                ltExisting.Text = string.Format("{0}/url/{1}", SPContext.Current.Site.RootWeb.Url, shortcode);
                phExisting.Visible = true;
                phCreate.Visible = false;
            }
            else
            {
                phExisting.Visible = false;
                phCreate.Visible = true;
            }
        }

        void btnCreate_Click(object sender, EventArgs e)
        {
            var shortCode = txtShortcode.Text;
            var url = QS_Url;

            if (string.IsNullOrEmpty(shortCode) || string.IsNullOrEmpty(url))
            {
                ltErrors.Text = "Missing Short Code or Url";
                return;
            }

            var urlShortenerRepository = new UrlShortenerRepository();
            if (urlShortenerRepository.CreateShortener(shortCode, url))
            {
                SetupSuccessPanel(string.Format("{2}<br/>{0}/url/{1}", SPContext.Current.Site.RootWeb.Url, txtShortcode.Text, "The short code has been created"));
            }
            else
            {
                ltErrors.Text = "Problem Short Url";
            }
        }
        
        void GenerateDefaultShortcode()
        {
            var urlShortenerRepository = new UrlShortenerRepository();
            txtShortcode.Text = urlShortenerRepository.GenerateShortCode();
        }

        void SetupSuccessPanel(string msg)
        {
            phSuccess.Visible = true;
            phCreate.Visible = false;
            phExisting.Visible = false;

            ltSuccessMessage.Text = msg;
        }

        string QS_Url
        {
            get { return Context.Request.QueryString["ItemUrl"] ?? string.Empty; }
        }

        string QS_Type
        {
            get
            {
                switch(Context.Request.QueryString["Type"] ?? string.Empty)
                {
                    case "List":
                    case "list":
                        return "List/Doc Lib";
                    case "Item":
                    case "item":
                        return "Item/Doc";
                    case "Folder":
                    case "folder":
                        return "Folder";
                    default:
                        return "?";
                }
                
            }
        }
    }
}
